//
//  GYXMeituanRefreshView.swift
//  RefreshControl
//
//  Created by Twisal on 2017/3/29.
//  Copyright © 2017年 Twisal. All rights reserved.
//

import UIKit
private let kEarthHeight: CGFloat = 25
private let height: CGFloat = 126
class GYXMeituanRefreshView: GYXRefreshView {

    /// 建筑
    @IBOutlet weak var buildingIconView: UIImageView!
    
    /// 地球
    @IBOutlet weak var earthView: UIImageView!
   
    /// 袋鼠
    @IBOutlet weak var kangarooIconView: UIImageView!

    
    override var viewHeight: CGFloat {
        didSet {
            
            if viewHeight < kEarthHeight {
                return
            }
            
            // 高度差 / 最大高度差
            var scale: CGFloat
            
            if viewHeight > self.bounds.height {
                scale = 1.0
            } else {
                scale = 1 - ((height - viewHeight) / (height - kEarthHeight))
            }
            kangarooIconView.transform = CGAffineTransform(scaleX: scale, y: scale)
            
        }
    }
    override func awakeFromNib() {
        // 1, 设置建筑的动画
        let image1 = #imageLiteral(resourceName: "icon_building_loading_1")
        let image2 = #imageLiteral(resourceName: "icon_building_loading_2")
        buildingIconView.image = UIImage.animatedImage(with: [image1, image2], duration: 0.5)
        
        // 2, 设置地球的旋转动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = -2 * M_PI
        anim.duration = 3
        anim.repeatCount = MAXFLOAT
        earthView.layer.add(anim, forKey: nil)
        
        // 3, 设置袋鼠的大小变化以及动画
       
        // 1> 设置缩放比例
//        kangarooIconView.transform = CGAffineTransform(scaleX: 0.4, y: 0.4)
        // 2> 设置锚点, 必须要设置frame才有效
        kangarooIconView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        
        let x = self.bounds.width / 2
        let y = self.bounds.height - kEarthHeight
        kangarooIconView.center = CGPoint(x: x, y: y)
        // 3> 设置动画
        let kImage1 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_1")
        let kImage2 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_2")
        kangarooIconView.image = UIImage.animatedImage(with: [kImage1, kImage2], duration: 0.5)
        
        
    }
}
